package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.RequiredPermission;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    @RequiresPermissions(value={"部门列表","department:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("pageInfo", departmentService.query(qo));
        return "department/list";
    }
    @RequiresPermissions(value={"部门编辑","department:input"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", departmentService.get(id));
        }
        return "department/input";
    }

    @RequiresPermissions(value={"部门保存或更新","department:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department entity) {

        JsonResult result = new JsonResult();
        try {
            //int a=1/0;
            departmentService.saveOrUpdate(entity);
        }catch (Exception e)
        {
//            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }
    @RequiresPermissions(value={"部门删除","department:delete"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {

            departmentService.delete(id);
        }catch (Exception e)
        {
            result.mark("亲,执行失败!");
        }
        return result;
    }
}
